<?php

class Api_ExpansionRequestController extends Zend_Rest_Controller
{
	private $db;
	private $templateSearchArray;
	
    public function init()
    {
        $this->_helper->layout()->disableLayout();
    	$this->_helper->viewRenderer->setNoRender(true);
    	// Hole den standardmäßigen DB Adapter (definiert in der Boootstrap.ini)
		$this->db = Zend_Db_Table::getDefaultAdapter();
		
		$this->templateSearchArray = array();
    	$this->templateSearchArray['serviceType']='';
    	// Dienst-Übergreifende Vorgaben
    	$this->templateSearchArray['constraints']['metaDatenSchutz'] = '';
		$this->templateSearchArray['constraints']['enddienst'] = '';
		$this->templateSearchArray['constraints']['logging'] = '';
		$this->templateSearchArray['constraints']['trustLevel'] = '';
		$this->templateSearchArray['constraints']['trustArchitektur'] = '';
		$this->templateSearchArray['constraints']['credentialArt'] = '';
		$this->templateSearchArray['constraints']['credentialDynamik'] = '';
		
		// Compliance
		$this->templateSearchArray['constraints']['euRichtlinien'] = '';
		$this->templateSearchArray['constraints']['bundesDatenschutz'] = '';
		$this->templateSearchArray['constraints']['usa'] = '';
		$this->templateSearchArray['constraints']['iso27001Zert'] = '';
		$this->templateSearchArray['constraints']['iso27002'] = '';
		$this->templateSearchArray['constraints']['bsiGrundschutz'] = '';
		$this->templateSearchArray['constraints']['penetrationsTests'] = '';
		$this->templateSearchArray['constraints']['pci'] = '';
		$this->templateSearchArray['constraints']['glba'] = '';
		$this->templateSearchArray['constraints']['hipaa'] = '';
		$this->templateSearchArray['constraints']['qualifikationITSiBe'] = '';
        
		// IAAS
		$this->templateSearchArray['constraints']['closeBoxVM'] = '';
		$this->templateSearchArray['constraints']['minimalTrustedComputingBase'] = '';	
		$this->templateSearchArray['constraints']['trustedPlatformModule'] = '';	
		$this->templateSearchArray['constraints']['geographischerSpeicherOrt'] = '';
		$this->templateSearchArray['constraints']['verschluesselung'] = '';
		$this->templateSearchArray['constraints']['zugriffsrichtlinien'] = '';
		$this->templateSearchArray['constraints']['backupDataRecovery'] = '';
		
      	// PAAS
      	$this->templateSearchArray['constraints']['updaterichtlinien'] = '';
      	
      	// SAAS
      	$this->templateSearchArray['constraints']['commonCriteriaStufe'] = '';
		$this->templateSearchArray['constraints']['nachrichtenIntegritaet'] = '';
		$this->templateSearchArray['constraints']['nachrichtenVertraulichkeit'] = '';
    }
    
    public function getTemplateSearchArray(){
    	return $this->templateSearchArray;
    }

    public function indexAction(){
    	
    }

	 public function getAction()
    { 	
    	$params = $this->getRequest()->getParams();
    	$profileid = $params['profile'];
    	
    	//DB Abfrage
    	  	
    	$select = new Zend_Db_Select($this->db);
    	$select->from(array('sp'=>'ServiceProfile'))
			->join(array('o'=>'Offers'), 'o.Offers_ID= sp.fk_Offers')
			->join(array('st'=>'ServiceType'), 'sp.fk_ServiceType = st.ServiceTypes_ID')
			->where('sp.ServiceProfile_ID=' .'"' . $profileid. '"' );
			
		$offerSet = $this->db->fetchAll($select);
    	
		$searchArray = $this->buildSearchArray($offerSet);

		
		require_once __DIR__.'/../../../controllers/utilities/ServiceMediator.php';
		$mediator = new ServiceMediator($searchArray);
    	
    	$result = $mediator->giveMatch();
		//Zend_Debug::dump($result);
    	
		//XML AUFBAUEN
		require_once __DIR__.'/../../../controllers/utilities/xmlutils.php';
		$xml = xmlutils::array_to_xml($result);
		
		
 
       	Zend_Debug::dump($xml);
		$doc = new DOMDocument('1.0');
    	$doc->preserveWhiteSpace = false;
    	$doc->loadXML($xml);
    	$doc->formatOutput = true;
   		$newxml = $doc->saveXML();
       	
		
		
        $this->getResponse()
        	->setHttpResponseCode(200)
            ->appendBody($newxml);
            

    }
    
    public function postAction()
    {
    	
    	//Rückgabe
    	$this->getResponse()
    		->appendBody("From postAction, RequestController")
    		->setHttpResponseCode(201);  	
    		    	

    }
    
    public function putAction()
    {
        $this->getResponse()
            ->appendBody("From putAction(), RequestController")
            ->setHttpResponseCode(200);

    }
    
    public function deleteAction()
    {
    	
        $this->getResponse()
            ->appendBody("From deleteAction(), RequestController");

    }
    
    private function buildSearchArray($offerSet){
    	
    	$searchArray = $this->templateSearchArray;
    	$searchArray['serviceType']=$offerSet[0]['serviceBeschreibung'];
    	
    	foreach ($searchArray['constraints'] as $key => $value) {
    		$searchArray['constraints'][$key] = $offerSet[0][$key];
    	}
    	
    	return $searchArray;
    	
    }




	public function assocArrayToXML($root_element_name,$ar){
    $xml = new SimpleXMLElement("<?xml version=\"1.0\"?><{$root_element_name}></{$root_element_name}>");
    $f = create_function('$f,$c,$a','
            foreach($a as $k=>$v) {
                if(is_array($v)) {
                    $ch=$c->addChild($k);
                    $f($f,$ch,$v);
                } else {
                    $c->addChild($k,$v);
                }
            }');
    $f($f,$xml,$ar);
    return $xml->asXML();
    

	} 

}